
//
//  ZJVideoLiveView.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/12.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJVideoLiveView.h"
#import "VHallApi.h"

#define VHALL_ACCOUNT @"third"//账号
#define VHALL_PASS @"11111111"//密码

@interface ZJVideoLiveView ()<VHallMoviePlayerDelegate>
/** 封面 */
@property (nonatomic, strong) UIImageView *imageView;
/** 播放按钮 */
@property (nonatomic, strong) UIImageView *live;

/** 视频控制模块背景 */
@property (nonatomic, strong) UIView *bgMeunView;
/** 时间label */
@property (nonatomic, strong) UILabel *timeLabel;
/** 全屏按钮 */
@property (nonatomic, strong) UIButton *fullScreenButton;
/** 视频参数 */
@property (nonatomic, strong) NSMutableDictionary * videoParam;

@end


@implementation ZJVideoLiveView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //监控旋转
        [self addOrientationObserver];
        
        self.backgroundColor = [UIColor blackColor];
        self.imageView.userInteractionEnabled = YES;
        [self addSubview:self.bgMeunView];
        [self.bgMeunView addSubview:self.timeLabel];
        [self.bgMeunView addSubview:self.fullScreenButton];
        self.bgMeunView.alpha = 0;
        
        [self addSubview:self.imageView];
        
        [self addSubview:self.live];
        //默认图片播放
        UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_startLive)];
        [self.imageView addGestureRecognizer:tapGester];
        
        [self addSubview:self.liveVideoPlayer.moviePlayerView];
        [self sendSubviewToBack:self.liveVideoPlayer.moviePlayerView];
       //添加隐藏、显示控制栏的收缩
        UITapGestureRecognizer *hiddenTapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_hiddenMeum)];
        [self.liveVideoPlayer.moviePlayerView addGestureRecognizer:hiddenTapGest];
        
        self.liveVideoPlayer.moviePlayerView.sd_layout
        .topSpaceToView(self, 0)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .bottomSpaceToView(self, 0);
        
        self.imageView.sd_layout
        .topSpaceToView(self, 0)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .bottomSpaceToView(self, 0);
        
        self.live.sd_layout
        .centerXEqualToView(self.imageView)
        .centerYEqualToView(self.imageView)
        .widthIs(70)
        .heightEqualToWidth();
        
        self.bgMeunView.sd_layout
        .bottomSpaceToView(self, 0)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .heightIs(44 * [UIScreen zj_screenHScale]);
        
        self.fullScreenButton.sd_layout
        .topSpaceToView(self.bgMeunView, 0)
        .heightRatioToView(self.bgMeunView, 1)
        .rightSpaceToView(self.bgMeunView, 12)
        .widthEqualToHeight();
        
        self.timeLabel.sd_layout
        .topSpaceToView(self.bgMeunView, 0)
        .heightRatioToView(self.bgMeunView, 1)
        .leftSpaceToView(self.bgMeunView, 12)
        .rightSpaceToView(self.fullScreenButton, 0);
        
        [self loginVHall];
    
    }
    return self;
}


- (void)loginVHall{
    [VHallApi loginWithAccount:VHALL_ACCOUNT password:VHALL_PASS success:^{
        [self.videoParam setObject:[VHallApi currentUserNickName] forKey:@"pass"];
        [self.videoParam setObject:@"" forKey:@"name"];
    } failure:^(NSError *error) {
        
    }];
}

//开始直播
- (void)action_startLive{
    [self meunViewAnaminIsHidden:NO];
    self.imageView.hidden = YES;
    self.live.hidden = YES;
    [self.liveVideoPlayer startPlay:self.videoParam];
}

- (void)action_hiddenMeum{
    if (self.bgMeunView.alpha == 1.0) {//当前为显示
        [self meunViewAnaminIsHidden:YES];
    }else{
        [self meunViewAnaminIsHidden:NO];
    }

}

//全屏按钮
- (void)action_fullScreenButton:(UIButton *)button{
    button.selected = !button.selected;
    if (self.delegate) {
        [self.delegate ZJVideoLiveViewDelegate_isFullScreen:button.selected isButtonClick:YES];
    }
}

- (void)meunViewAnaminIsHidden:(BOOL)isHidden{
    __weak typeof(self) weakSelf = self;
    if (isHidden) {//控制栏隐藏
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.bgMeunView.alpha = 0.0;
        }];
    }else{//控制栏显示
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.bgMeunView.alpha = 1.0;
        }completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.bgMeunView.alpha == 1.0) {
                    [UIView animateWithDuration:0.3 animations:^{
                        weakSelf.bgMeunView.alpha = 0.0;
                    }];
                }
            });
            
        }];
    }
}

-  (void)addOrientationObserver{
    UIDevice *device = [UIDevice currentDevice];
    [device beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:device];
}

- (void)orientationChanged:(NSNotification *)note{
    //	NSLog(@"%s", __FUNCTION__);
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIInterfaceOrientationPortrait) {
        //竖屏
        if (self.delegate) {
            self.fullScreenButton.selected = NO;
            [self.delegate ZJVideoLiveViewDelegate_isFullScreen:NO isButtonClick:NO];
            
        }
      
    }else if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
       //横屏
        if (self.delegate) {
            self.fullScreenButton.selected = YES;
            [self.delegate ZJVideoLiveViewDelegate_isFullScreen:YES isButtonClick:NO];
            
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Setter
- (void)setVideoID:(NSString *)videoID{
    _videoID = videoID;
    [self.videoParam setObject:videoID forKey:@"id"];
}
- (void)setImageUrlString:(NSString *)ImageUrlString{
    _ImageUrlString = ImageUrlString;
    if ([ImageUrlString containsString:@"http"]) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:ImageUrlString] placeholderImage:PlaceholderImage];
    }else{
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,self.ImageUrlString]] placeholderImage:PlaceholderImage];
    }
    
}

#pragma mark - Getters
- (UIImageView *)imageView{
    if (_imageView == nil){
        _imageView = [UIImageView new];
        
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageView;
}
- (UIImageView *)live{
    if (_live == nil){
        _live = [UIImageView new];
        _live.contentMode = UIViewContentModeCenter;
    }
    return _live;
}

- (UIView *)bgMeunView{
    if (_bgMeunView == nil){
        _bgMeunView = [UIView new];
        _bgMeunView.backgroundColor = [UIColor zj_colorWithHexString:@"ffffff" alpha:0.1];
        
    }
    return _bgMeunView;
}
- (UILabel *)timeLabel{
    if (_timeLabel == nil){
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont subTitleTextFont];
        _timeLabel.textColor = [UIColor whiteColor];
        _fullScreenButton.backgroundColor = [UIColor orangeColor];
    }
    return _timeLabel;
}

- (UIButton *)fullScreenButton{
    if (_fullScreenButton == nil){
        _fullScreenButton = [UIButton new];
        [_fullScreenButton setImage:[UIImage imageNamed:@"pl-video-player-fullscreen"] forState:UIControlStateNormal];
        [_fullScreenButton setImage:[UIImage imageNamed:@"pl-video-player-shrinkscreen"] forState:UIControlStateSelected];
        [_fullScreenButton addTarget:self action:@selector(action_fullScreenButton:) forControlEvents:UIControlEventTouchUpInside];
        _fullScreenButton.selected = NO;
        _fullScreenButton.backgroundColor = [UIColor clearColor];
    }
    return _fullScreenButton;
}

- (VHallMoviePlayer *)liveVideoPlayer{
    if (_liveVideoPlayer == nil){
        _liveVideoPlayer = [[VHallMoviePlayer alloc] initWithDelegate:self];
        _liveVideoPlayer.movieScalingMode = kRTMPMovieScalingModeAspectFit;
//        _liveVideoPlayer.bufferTime = 10;
        _liveVideoPlayer.reConnectTimes = 2;
    }
    return _liveVideoPlayer;
}

- (NSMutableDictionary *)videoParam{
    if (_videoParam == nil){
        _videoParam = [NSMutableDictionary dictionary];
        
    }
    return _videoParam;
}

@end

